
  /**************************************************************************/
  /*                                                                        */
  /*  System  Name :  ICIM                                                  */
  /*                                                                        */
  /*  Description  :  Operation Station Management                          */
  /*                                                                        */
  /*  MODIFICATION HISTORY                                                  */
  /*    Date     Ver     Name          Description                          */
  /* ---------- ----- ----------- ----------------------------------------- */
  /* 2017/10/12 N0.00                   Initial release                     */
  /*                                                                        */
  /**************************************************************************/

$(document).ready(function() {

	var VAL = {
			T_FIPLSTOPE : "FIPLSTOPE",
			NORMAL : "0000000",
       		EVT_USER   : $("#userId").text(),
			DISABLED_ATTR : {
				"disabled" : true
			},
			ENABLED_ATTR : {
				"disabled" : false
			}
		};

    /**
     * All controls's jquery object/text
     * @type {Object}
     */
	var controlsQuery = { 
		W   : $(window),
		$opeIDTxt : $("#opeIDTxt"),
		$opeVerTxt : $("#opeVerTxt"),
		$opeDscTxt : $("#opeDscTxt"),
		$procIDSel : $("#procIDSel"),
		$toolgIDSel:$("#toolgIDSel"),
		$deptIDSel : $("#deptIDSel"),
		$upLoadIDTxt : $("#upLoadIDTxt"),
		$dnLoadIDTxt : $("#dnLoadIDTxt"),
		$ITOflgSel   : $("#ITOflgSel"),
		
		mainGrd   :{
            grdId     : $("#opeListGrd")   ,
            grdPgText : "#opeListPg"       ,
            fatherDiv : $("#opeListDiv")
        }
	};

    /**
     * All button's jquery object
     * @type {Object}
     */
    var btnQuery = {
        query_btn : $("#query_btn"),
        delete_btn : $("#delete_btn"),
        add_btn : $("#add_btn"),
        save_btn : $("#save_btn"),
        update_btn : $("#update_btn"),
        copy_btn : $("#copy_btn"),
        rollback_btn : $("#rollback_btn"),
        clear_btn : $("#clear_btn")
    };
	
    /**
     * All tool functions
     * @type {Object}
     */
    var toolFunc = {
	   	resizeFnc : function(){ 
	        var offsetBottom, divWidth;
	        divWidth = controlsQuery.mainGrd.fatherDiv.width();
	        offsetBottom = controlsQuery.W.height() - controlsQuery.mainGrd.fatherDiv.offset().top;
	        controlsQuery.mainGrd.fatherDiv.height(offsetBottom);
	        controlsQuery.mainGrd.grdId.setGridWidth(divWidth);
	        controlsQuery.mainGrd.grdId.setGridHeight(offsetBottom * 0.99 - 101);
	    },
	    initFnc : function(){
	        $("input").val("");
	        $("select").empty();
	        $("#opeConditionForm input").attr(VAL.DISABLED_ATTR);
			$("#opeConditionForm select").attr(VAL.DISABLED_ATTR);
	    },
	    iniPRSTSelect : function(){
	    	comAddValueByDataCateFnc("#procIDSel","PRST","data_ext","data_desc",true);
	    },
	    iniDEPTSelect : function(){
	    	comAddValueByDataCateFnc("#deptIDSel","DEPT","data_ext","data_desc",true);
	    },
        iniTOOLGSelect : function(){
            comAddValueByDataCateFnc("#toolgIDSel","TOLG","data_ext","data_desc",true);
        },
		com_get_1st_inf : function(){
			var rowIds = controlsQuery.mainGrd.grdId.jqGrid('getDataIDs');
			if(rowIds.length>0){
				controlsQuery.mainGrd.grdId.setSelection(rowIds[0], true);
			}
		},
		opeGridSelRowFnc : function(rowId) {
			if(controlsQuery.$opeDscTxt.attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var rowData, mdlId, iary, inObj, outObj, oary, i, tblCnt, oaryArr;
			$("input").attr(VAL.DISABLED_ATTR);
			$("select").attr(VAL.DISABLED_ATTR);
			
			rowData = controlsQuery.mainGrd.grdId.jqGrid("getRowData", rowId);
			ope_id = rowData.ope_id;

			inObj = {
				trx_id : T_FBPBISOPE,
				action_flg : "Q",
				iary : [{
					ope_id : ope_id
				}]
			};
			outObj = comTrxSubSendPostJson(inObj);
			if (outObj.rtn_code == VAL.NORMAL) {
				oary = outObj.oary[0];
				if (!oary) {
					return false;
				}

				controlsQuery.$opeIDTxt.val(oary.ope_id.trim());
				
				SelectDom.setSelect(controlsQuery.$procIDSel, oary.proc_id.trim());
				SelectDom.setSelect(controlsQuery.$deptIDSel, oary.dept_id.trim());
                SelectDom.setSelect(controlsQuery.$toolgIDSel, oary.toolg_id.trim());
				controlsQuery.$opeDscTxt.val(oary.ope_dsc.trim());
			}
		}
    };
    
    /**
     * All button click function
     * @type {Object}
     */
    var btnFunc = {
    	query_func : function(){
			if($("#opeDscTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
    		/*** 将Div实例化为modal窗体 ***/
    	    function diaLogQueryFnc(){
    	    	var iary = {};
    	    	if($("#queryOpeDialog_opeIDTxt").val()!=""){
    	    		iary.ope_id  = $("#queryOpeDialog_opeIDTxt").val() ;
    	    	}
    	    	
    	    	if($("#queryOpeDialog_opeVerTxt").val()!=""){
    	    		iary.ope_ver = $("#queryOpeDialog_opeVerTxt").val();
    	    	}
    	    	
    	    	 var inTrxObj ={
    	    			 trx_id     : T_FBPBISOPE ,
    	    			 action_flg : "Q"        ,
    	    			 iary       : [iary]
    	    	 }
    	    	 
    	    	 var  outTrxObj = comTrxSubSendPostJson(inTrxObj);
    	    	 if(  outTrxObj.rtn_code == _NORMAL ) {
    	    		 setGridInfo(outTrxObj.oary,"#opeListGrd");
    	    		 $('#queryOpeDialog').modal("hide");
    	    	 }
    	    }
    	         
    	    $('#queryOpeDialog').modal({
    	        backdrop:true,
    	        keyboard:false,
    	        show:false
    	    });
    	    $('#queryOpeDialog').unbind('shown.bs.modal');
    	    $("#queryOpeDialog_queryBtn").unbind('click');

    	    $('#queryOpeDialog').modal("show");
    	    $("#queryOpeDialog_queryBtn").bind('click',diaLogQueryFnc);
    	    $("#queryOpeDialog_opeIDTxt").attr({'disabled':false});
    	    $("#queryOpeDialog_opeVerTxt").attr({'disabled':false});
    	    $("#queryOpeDialog_opeIDTxt").val("");
    	    $("#queryOpeDialog_opeVerTxt").val("");
    	},
    	delete_func : function(){
			if($("#opeDscTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
    		var selectRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
    	    if(!selectRowId){
    	    	showErrorDialog("001","请选择需要删除的站点！");
    	    	return false;
    	    } 
    	    var rowData = controlsQuery.mainGrd.grdId.jqGrid("getRowData",selectRowId);
    	    $("#f1_query_btn").showCallBackWarnningDialog({
    	    	errMsg  : "是否删除代码为:["+ rowData.ope_id +"]的站点，请确认!!!!",
    	    	callbackFn : function(data) {
    	    		if(data.result==true){
    	    			var iary = [{
    	    					ope_id : rowData.ope_id,
    	    					ope_ver: '00000'
    	    			}]
    	    			var inTrxObj={
    	    					trx_id     : T_FBPBISOPE,
    	    					action_flg : "D"  ,
    	    					iary       : iary
    	    			}
    	    			var outTrxObj = comTrxSubSendPostJson(inTrxObj);
    	    			if(outTrxObj.rtn_code === _NORMAL){
    	    				showSuccessDialog("站点信息删除成功！！！");
    	    				$("#opeListGrd").jqGrid("delRowData",selectRowId);
        	                $("#opeConditionForm input").val("");
        	                SelectDom.setSelect($("select"), "", "");
    	    			}
    	    		}
    	    	}
    	    });
    	},
    	add_func : function(){
    		if($("#opeIDTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "已经在新增中，请勿重复新增");
				return false;
			}
			if($("#opeDscTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
    		$("input[type='text']").val("");
            SelectDom.setSelect($("select"),"","");
            SelectDom.setSelect(controlsQuery.$deptIDSel, "", "");
            SelectDom.setSelect(controlsQuery.$toolgIDSel, "", "");
    		iniContorlData;
    		$("#opeConditionForm input").attr(VAL.ENABLED_ATTR);
			$("#opeConditionForm select").attr(VAL.ENABLED_ATTR);
    		$("#opeIDTxt").focus();
    		
    	},
    	save_func : function(){
    		var actionFlg, inObj, outObj, iary, newRowId;
    		var opeID,opeVer,opeDsc,procID,toolRunMode,deptID,toolgID,upLoadID,dnLoadID;
    		actionFlg = $("#opeIDTxt").attr("disabled") === "disabled" ? "U" : "A";
    		opeID = $("#opeIDTxt").val().trim();
    		opeDsc = $("#opeDscTxt").val().trim();
    		procID = $("#procIDSel").val().trim() == null?"":$("#procIDSel").val().trim();
    		deptID = $("#deptIDSel").val().trim() == null?"":$("#deptIDSel").val().trim();
            toolgID = $("#toolgIDSel").val().trim() == null?"":$("#toolgIDSel").val().trim();
    		
    		if (!opeID) {
				showErrorDialog("", "站点代码不能为空");
				return false;
			}
    		if (!opeDsc) {
				showErrorDialog("", "站点描述不能为空");
				return false;
			}
    		if (!procID) {
				showErrorDialog("", "制程代码不能为空");
				return false;
			}
            if (!toolgID) {
                showErrorDialog("", "设备群组不能为空");
                return false;
            }
    		if (!deptID) {
				showErrorDialog("", "部门代码不能为空");
				return false;
			}
    		iary = {
    			ope_id : opeID,
    			ope_ver : '00000',
    			ope_dsc :  opeDsc,     
    			proc_id : procID,
    			tool_run_mode : toolRunMode,
    			dept_id : deptID,
    			toolg_id:toolgID,
    			up_load_id : upLoadID,   
    			dn_load_id : dnLoadID  
    		}
    		inObj = {
    				trx_id : T_FBPBISOPE,
    				action_flg : actionFlg,
    				evt_usr : VAL.EVT_USER,
    				iary : [iary]
    			};
    		var outObj = comTrxSubSendPostJson(inObj);
    		if (outObj.rtn_code == VAL.NORMAL) {
				
				$("input").attr(VAL.DISABLED_ATTR);
				$("select").attr(VAL.DISABLED_ATTR);
				
				if (actionFlg == "A") {
					var newRowId = getGridNewRowID("#opeListGrd");
					controlsQuery.mainGrd.grdId.jqGrid("addRowData", newRowId,iary);
					controlsQuery.mainGrd.grdId.setSelection(newRowId, true); 
					showSuccessDialog("新增站点信息成功");
					
				} else if (actionFlg == "U") {
					var rowid = controlsQuery.mainGrd.grdId.jqGrid('getGridParam','selrow');
					toolFunc.opeGridSelRowFnc(rowid);
					showSuccessDialog("站点信息更新成功");
				}
			}
    		
    	},
    	update_func : function() {
			if($("#opeDscTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var selRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要修改的站点信息");
				return false;
			}
			$("#opeConditionForm input:not(#opeIDTxt,#opeVerTxt)").attr(VAL.ENABLED_ATTR);
			$("#opeConditionForm select").attr(VAL.ENABLED_ATTR);
			controlsQuery.$opeDscTxt.focus();
		},
		copy_func:function(){
			if($("#opeDscTxt").attr("disabled") != "disabled"){
				showErrorDialog("", "目前是编辑状态，不可进行其它操作！");
				return false;
			}
			var selRowId = controlsQuery.mainGrd.grdId.jqGrid("getGridParam","selrow");
			if (!selRowId) {
				showErrorDialog("", "请选择需要复制的站点信息");
				return false;
			}
			$("#opeConditionForm input").attr(VAL.ENABLED_ATTR);
			$("#opeConditionForm select").attr(VAL.ENABLED_ATTR);
			controlsQuery.$opeIDTxt.focus();
		},
		rollback_func : function(){
			if($("#opeDscTxt").attr("disabled") == "disabled"){
				showErrorDialog("", "没有编辑，无需撤回");
				return false;
			}
			var outObj,inObj_Query,opeId,iary ={};
			opeId = $("#opeIDTxt").val();
			if(opeId){
				iary.opeId = opeId;
			}
			var inObj_Query = {
						trx_id : T_FBPBISOPE,
						action_flg : 'Q',
						iary : [iary]
					};
			$("#opeConditionForm input").attr(VAL.DISABLED_ATTR);
			$("#opeConditionForm select").attr(VAL.DISABLED_ATTR);
			outObj = comTrxSubSendPostJson(inObj_Query);
			if (outObj.rtn_code === VAL.NORMAL) {
				setGridInfo(outObj.oary, "#opeListGrd");
				$("#queryOpeDialog").modal("hide");
				toolFunc.com_get_1st_inf();
			}
		},
		clear_func : function() {
			$("input[type='text']").val("");
			SelectDom.setSelect($("select"), "", "");
            SelectDom.setSelect(controlsQuery.$deptIDSel, "", "");
            SelectDom.setSelect(controlsQuery.$toolgIDSel, "", "");
            controlsQuery.mainGrd.grdId.jqGrid("clearGridData");

            $("input").attr(VAL.DISABLED_ATTR);
            $("select").attr(VAL.DISABLED_ATTR);
		}

    };
    
    /**
     * grid  initialization
     */
    var iniGridInfo = function(){
        controlsQuery.mainGrd.grdId.jqGrid({
        	url:"",
       		datatype : "local",
    		mtype : "POST",
    		height : controlsQuery.mainGrd.fatherDiv.height(),
    		autowidth : true,//宽度根据父元素自适应
            shrinkToFit:false,
    		scroll : true,
    		resizable : true,
    		rownumbers : true,
    		loadonce : true,
    		viewrecords : true,
    		colModel : [{name: 'ope_id'       , index: 'ope_id'     , label: OPE_ID_TAG     , width: 200 },
    		            {name: 'ope_dsc'      , index: 'ope_dsc'    , label: OPE_DSC_TAG    , width: 280 }],
    		pager : controlsQuery.mainGrd.grdPgText,
    		onSelectRow : function(id) {
    			toolFunc.opeGridSelRowFnc(id);
			}
    	});
    };
    
    /**
     * Bind button click action
     */
    var iniButtonAction = function(){
        btnQuery.query_btn.click(function(){
            btnFunc.query_func();
        });
        btnQuery.delete_btn.click(function(){
            btnFunc.delete_func();
        });
        btnQuery.add_btn.click(function(){
            btnFunc.add_func();
        });
        btnQuery.save_btn.click(function(){
            btnFunc.save_func();
        });
        btnQuery.update_btn.click(function(){
            btnFunc.update_func();
        });
        btnQuery.copy_btn.click(function(){
            btnFunc.copy_func();
        });
        btnQuery.rollback_btn.click(function(){
            btnFunc.rollback_func();
        });
        btnQuery.clear_btn.click(function(){
            btnFunc.clear_func();
        });
    };

    /**
     * Ini contorl's data
     */
    var iniContorlData = function(){
    	toolFunc.initFnc();
        toolFunc.resizeFnc();
        toolFunc.iniPRSTSelect();
        toolFunc.iniDEPTSelect();
        toolFunc.iniTOOLGSelect();
    };
    
    var clear = function(){
    	controlsQuery.mainGrd.grdId.jqGrid("clearGridData");
		$("input[type='text']").val("");
		$("select").empty();
    };
    
    var otherActionBind = function(){
        //Stop from auto commit
        $("form").submit(function(){
            return false;
        });
        
        controlsQuery.W.resize(function() {
        	toolFunc.resizeFnc();
    	});
        
    };
    
    /**
     * Ini view, data and action bind
     */
    var initializationFunc = function(){
    	iniGridInfo();
        iniButtonAction();
        otherActionBind();
        iniContorlData();
    };
    
    initializationFunc();
    //enter
    /*document.onkeydown=function(event){
        var e = event || window.event || arguments.callee.caller.arguments[0];
        if(e && e.keyCode==13){ // 按 enter
            $('.bootbox').modal("hide");
        }
    }*/
})